*******************************************************************************************************************************
			******************************************************************************************************
			
			**Do file for "Measuring Effective Democracy", Knutsen (2010) International Political Science Review**
			
			******************************************************************************************************
*******************************************************************************************************************************

**Please note that there are some small deviations in the estimates generated by running this do-file on the dataset attached. This is due to the reconstruction of the dataset, as the latest version of the original dataset was not kept. There is a slight deviation in the number of observations between the original analysis and the analysis based on the reconstructed version, and I have not been able to find the source of this deviation. However, the final results are very similar, and are even a bit "stronger" for the analysis based on the new data set.

**For descriptions of the Polity Index (variable name: Politynonanarchyetc), see Marshall and Jaggers (2002). Notice that I have excluded from the Polity Index countries that are coded as -66, -77 and -88.
**For descriptions of the Vanhanen data (variable name: vhid), see: http://www.prio.no/CSCW/Datasets/Governance/Vanhanens-index-of-democracy/Polyarchy-Dataset-Manuscript/
**For descriptions of the Freedom House data (variable name: AggregFHI), see: http://www.freedomhouse.org/template.cfm?page=351&ana_page=363&year=2010     Notice that the variables is an average of Freedom Houses Political Rights and Civil Liberties indexes.
**For descriptions of the World Governance Indicator's Control of Corruption Index (variable name: wgicorrupt), see: http://papers.ssrn.com/sol3/papers.cfm?abstract_id=999979
**For descriptions of the PPP-adjusted GDP data from the World Development Indicators (variable name: WDIGDPPP), see: http://data.worldbank.org/indicator/NY.GDP.PCAP.PP.KD
**For the operationalizations underlying the Western+ dummy (variable name: westeurplusoffshots) and the Protestant+ dummy (variable name: protestanglic), please see Appendix I in Knutsen (2007), which can be found here: http://folk.uio.no/carlhk/publications/MasterEconomicsDemocracyandPropertyRights.pdf


*************************
*************************
*************************

**Open data set; insert your own location


 *use "M:\Aførsteam\replication\MEDreplication.dta", clear

**Here are the variable transformation commands for constructing the Effective Democracy Index.

generate reversedFHI= 8-AggregFHI
generate normalizedrevFHI = 100*((reversedFHI-1)/6)
summarize wgicorrupt, detail
generate normalizedwgicorrupt = (wgicorrupt+2.084054)/4.657943
generate effectivedemocracy =  normalizedrevFHI*normalizedwgicorrupt
summarize effectivedemocracy, detail
**Notice that normalization is computed according to (X - min score)/(max score - min score), where X is an observation's value on the variable

**First, I run a principal component analysis using the four democracy indictators

pca reversedFHI Politynonanarchyetc effectivedemocracy vhid

**Thereafter, I run a factor analysis restricted to only one factor

factor reversedFHI Politynonanarchyetc effectivedemocracy vhid, factors(1)

**I thereafter instruct STATA to keep the predicted score on the democracy factor

predict democracyfactor, regression

**I then normalize the democracy factor, after finding the minimum and maximum scores: min. score democracyfactor = -1.560648 (Saudi Arabia 2000, 1998, 1996); max. score 1.652284 (Denmark 1996)

generate normdemocracyfactor = (democracyfactor + 1.560648)/(1.652284+1.560648)

**Then, I run OLS with PCSE (AR1 autocorrelation, heteroskedastic panels and contemporraneous correlation taken into account), with the EDI as dependent and the democracy factor as independent variable.

xtpcse effectivedemocracy democracyfactor, correlation(psar1) rhotype(tscorr) pairwise

**I instruct STATA to keep the linearly predicted EDI values 

predict predictedEDI, xb

** I generate the residuals by subtracting the predicted from the actual values

generate EDresidual = effectivedemocracy-predictedEDI

**Here is a scatterplot similar to that of Figure 2, which shows how the residuals are systematically correlated with PPP-adjusted GDP per capita

twoway (scatter EDresidual WDIGDPPP, sort) (lfit EDresidual WDIGDPPP) 

** I then run OLS with PCSE to find whether income level, being a Western and being a protestant country is systematically linked to the "bias" in the EDI. 
**Notice that I here run a OLS PCSE version that does not account for contemporraneous correlation, as STATA is unable to calculate the proper variance-covariance matrix (due to the short panels), but this does not matter much

xtpcse EDresidual WDIGDPPP westeurplusoffshots protestanglic, correlation(psar1) hetonly

**If you want to view the data that are used for the appendix table on my website (http://folk.uio.no/carlhk/publications/AppendixMED.pdf), then run the following command:

browse country normdemocracyfactor effectivedemocracy predictedEDI EDresidual country if year == 2000

***Drop the variables generated by do-file
drop  reversedFHI normalizedrevFHI normalizedwgicorrupt effectivedemocracy democracyfactor normdemocracyfactor predictedEDI EDresidual

*save data set, using your own location
*save "M:\Aførsteam\replication\MEDreplication.dta", replace